#include "stdafx.h"
#include "wf_modifytrimamount.h"

#include "pf_snapper.h"


WF_ModifyTrimAmount::WF_ModifyTrimAmount(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_ActionInterface("Trim Entity by a given amount",
                    container, graphicView) {

    trimEntity = NULL;
    trimCoord = PF_Vector(false);
    distance = 0.0;
}

QAction* WF_ModifyTrimAmount::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Lengthen")
	QAction* action = new QAction(tr("&Lengthen"), NULL);
	action->setIcon(QIcon(":/extui/modifytrimamount.png"));
	//action->zetStatusTip(tr("Lengthen by a given amount"));
	return action;
}


void WF_ModifyTrimAmount::init(int status) {
    PF_ActionInterface::init(status);

    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_ModifyTrimAmount::trigger() {

    PF_DEBUG->print("WF_ModifyTrimAmount::trigger()");

    if (trimEntity!=NULL && trimEntity->isAtomic()) {

        PF_Modification m(*container, graphicView);
        m.trimAmount(trimCoord, (PF_AtomicEntity*)trimEntity, distance);

        trimEntity = NULL;
        setStatus(ChooseTrimEntity);

        PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
}



void WF_ModifyTrimAmount::mouseReleaseEvent(PF_MouseEvent* e) {

    trimCoord = graphicView->toGraph(e->x(), e->y());
    trimEntity = catchEntity(e);

    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case ChooseTrimEntity:
            if (trimEntity!=NULL && trimEntity->isAtomic()) {
                trigger();
            } else {
                if (trimEntity==NULL) {
                    PF_DIALOGFACTORY->commandMessage(
                        tr("No entity found. "));
                } else if (trimEntity->rtti()==LX_Define::EntityInsert) {
                    PF_DIALOGFACTORY->commandMessage(
                        tr("The chosen Entity is in a block. "
                           "Please edit the block."));
                } else {
                    PF_DIALOGFACTORY->commandMessage(
                        tr("The chosen Entity is not an atomic entity "
                           "or cannot be trimmed."));
                }
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    }
}



void WF_ModifyTrimAmount::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case ChooseTrimEntity: {
            bool ok;
            double d = PF_Math::eval(c, &ok);
            if (ok==true) {
                distance = d;
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(ChooseTrimEntity);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_ModifyTrimAmount::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case ChooseTrimEntity:
        break;
    default:
        break;
    }

    return cmd;
}


void WF_ModifyTrimAmount::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_ModifyTrimAmount::hideOptions() {
    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_ModifyTrimAmount::updateMouseButtonHints() {
    switch (getStatus()) {
    case ChooseTrimEntity:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Select entity to trim or enter distance:"),
            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyTrimAmount::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyTrimAmount::updateToolBar() {
    PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
}


// EOF
